﻿using Microsoft.AspNet.OData;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace NETCORE.OdataRestful
{
    [Route("api/[controller]")]
    [ApiController]
    public class BooksController : ODataController
    {
        private static IList<Book> Books { get; set; }
        public BooksController()
        {
            Books = new List<Book>
        {
            new Book
            {
                Id = 1,
                IdLong=9060561917313025,
                ISBN = "111-0-321-56789-1",
                Title = "Calculus",
                Price = 66.6m,
                AddressList =new List<Address>(){ new Address
                {
                    City = "Shanghai",
                    Street = "Beijin Xi Road"
                }, new Address
                {
                    City = "Shanghai",
                    Street = "Beijin Xi Road"
                } },
                Press = new Press
                {
                    Id = 1,
                    Name = "Shanghai Tongji",
                    Category = Category.Book
                }
            },
            new Book
            {
                Id = 2,
                ISBN = "222-2-654-00000-2",
                Title = "Linear Algebra",
                Price = 53.2m,
                   AddressList =new List<Address>(){ new Address
                {
                    City = "Shanghai",
                    Street = "Beijin Xi Road"
                }, new Address
                {
                    City = "Shanghai",
                    Street = "Beijin Xi Road"
                } },
                Press = new Press
                {
                    Id = 2,
                    Name = "Shanghai Fudan",
                    Category = Category.EBook
                }
            }
        };
        }

        [EnableQuery]
        public IActionResult Get()
        {
            return Ok( Books);
             
        }



        [EnableQuery]
        public IActionResult Get([FromODataUri] int key)
        {
            return Ok(Books.FirstOrDefault(b => b.Id == key));
        }


        [EnableQuery]
        public IActionResult Post([FromBody] dto dd)
        {
            return Ok(dd);
        }


        [EnableQuery]
        public IActionResult Post([FromODataUri] int cc)
        {
            return Ok(cc);
        }


        [EnableQuery]
        public IActionResult Post(List<dto> dd)
        {
            return Ok(dd);
        }

        [EnableQuery]
        public IActionResult Delete([FromODataUri] int key)
        {
            return Ok(key);
        }
    }
    public class dto
    {
        public int icc { get; set; }
        public string name { get; set; }
    }

}
